﻿using AJWPFAdmin.Core.Enums;
using AJWPFAdmin.Core.Utils;
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace AJWPFAdmin.Core.Models.EF.Tables
{
    public partial class ShippingRecord
    {
        /// <summary>
        /// 关联车类型是否启用了车辆统计, 车牌识别到达后, 动态赋值
        /// </summary>
        [NotMapped]
        public bool EnablePassagewayStatistic { get; set; }

        /// <summary>
        /// 车辆基础台账是否同步成功, 进出厂车和固定车进出厂赋值
        /// </summary>
        [NotMapped]
        public bool CarSynchronized { get; set; }

        /// <summary>
        /// 是否是管理端配置的自动开闸还是要等待人工确认 --阿吉 2024年12月12日15点40分
        /// </summary>
        [NotMapped]
        public bool IsConfigAutoPass { get; set; }

        /// <summary>
        /// 黑名单查询是否异常或者网络异常
        /// </summary>
        [NotMapped]
        public bool BlackQueryError { get; set; }

        public void CompleteFileUrls(CarIdentificationConfig carIdentificationConfig, string propName)
        {

            var atts = GetFilesOfPropertyName(propName, out var prop);
            if (atts.Length > 0)
            {
                prop.SetValue(this, CommonUtil.AJSerializeObject(atts.Select(p => carIdentificationConfig.CompleteFileUrl(CarNo, p))));
            }
        }

        public string GetFileOfPropertyName(string propName)
        {
            var prop = this.GetType().GetRuntimeProperties()
                .FirstOrDefault(p => propName.Equals(p.Name, StringComparison.OrdinalIgnoreCase));
            if (prop == null)
            {
                return string.Empty;
            }

            var array = CommonUtil.TryGetJSONObject<string[]>(prop.GetValue(this) as string) ?? Array.Empty<string>();
            return array.FirstOrDefault() ?? string.Empty;
        }

        public string[] GetFilesOfPropertyName(string propName)
        {
            var prop = this.GetType().GetRuntimeProperties()
                .FirstOrDefault(p => propName.Equals(p.Name, StringComparison.OrdinalIgnoreCase));
            if (prop == null)
            {
                return Array.Empty<string>();
            }

            return CommonUtil.TryGetJSONObject<string[]>(prop.GetValue(this) as string) ?? Array.Empty<string>();
        }

        public string GetWatchPositionImageFile(PassagewayDirection direction, WatchPositionType watchPosition)
        {
            var source = direction == PassagewayDirection.进 ? InCaptureFile : OutCaptureFile;
            var array = CommonUtil.TryGetJSONObject<string[]>(source) ?? Array.Empty<string>();
            if (array.Length == 0)
            {
                return string.Empty;
            }

            foreach (var file in array)
            {
                var fileNameArray = Path.GetFileNameWithoutExtension(file).Split('_');
                if ("monitor".Equals(fileNameArray.ElementAtOrDefault(0) ?? string.Empty))
                {
                    var typeStr = fileNameArray.ElementAtOrDefault(1) ?? "unknown";
                    if (Enum.TryParse<WatchPositionType>(typeStr, true, out var ret)
                        && ret == watchPosition)
                    {
                        return file;
                    }
                }
            }

            return string.Empty;
        }

        public (string head, string plate, string tail) GetWatchPositionImageFile(PassagewayDirection direction)
        {
            var head = string.Empty;
            var plate = string.Empty;
            var tail = string.Empty;
            var source = direction == PassagewayDirection.进 ? InCaptureFile : OutCaptureFile;
            var array = CommonUtil.TryGetJSONObject<string[]>(source) ?? Array.Empty<string>();
            if (array.Length == 0)
            {
                return (head, plate, tail);
            }

            foreach (var file in array)
            {
                var fileNameArray = Path.GetFileNameWithoutExtension(file).Split('_');
                var first = fileNameArray.ElementAtOrDefault(0) ?? string.Empty;
                if ("full".Equals(first))
                {
                    head = file;
                    continue;
                }
                if ("clip".Equals(first))
                {
                    plate = file;
                    continue;
                }
                if ("monitor".Equals(first))
                {
                    var typeStr = fileNameArray.ElementAtOrDefault(1) ?? "unknown";
                    if (Enum.TryParse<WatchPositionType>(typeStr, true, out var ret))
                    {
                        if (string.IsNullOrWhiteSpace(head) && ret == WatchPositionType.车头)
                        {
                            head = file;
                            continue;
                        }
                        if (ret == WatchPositionType.车身)
                        {
                            tail = file;
                            continue;
                        }
                    }
                }
            }

            return (head, plate, tail);
        }

        public string[] GetCaptureFiles(PassagewayDirection direction)
        {
            var filesJson = direction == PassagewayDirection.进 ? InCaptureFile : OutCaptureFile;

            return CommonUtil.TryGetJSONObject<string[]>(filesJson) ?? Array.Empty<string>();
        }

        public string[] GetFilesOfPropertyName(string propName, out PropertyInfo propInfo)
        {
            propInfo = this.GetType().GetRuntimeProperties().FirstOrDefault(p => propName.Equals(p.Name, StringComparison.OrdinalIgnoreCase));
            if (propInfo == null)
            {
                return Array.Empty<string>();
            }
            return CommonUtil.TryGetJSONObject<string[]>(propInfo.GetValue(this) as string) ?? Array.Empty<string>();
        }
    }
}
